package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.adapter.MyBankListAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.MyBankProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyDialog;
import com.example.administrator.zhongyichou.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/11/30.
 * 描述：
 */

public class MyBankActivity extends BaseActivity<MyBankProtocol> implements MyBankListAdapter.OnDeleteClick {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.btn_bind)
    Button btnBind;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;

    private MyBankListAdapter adapter;
    private String cardID;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        listview.setEmptyView(llEmpty);
    }

    @Override
    protected void responseData(MyBankProtocol mGsonBaseProtocal) {
        cardID = mGsonBaseProtocal.getData().getCard_id();
        List<MyBankProtocol.DataBean> list = new ArrayList<>();
        if (mGsonBaseProtocal.getData()!=null) {
            list.add(mGsonBaseProtocal.getData());
        }
        adapter = new MyBankListAdapter(this, list);
        adapter.setOnDeleteClick(this);
        listview.setAdapter(adapter);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(MyBankProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        requestHttpLoader.setParams(NetConfig.USER_CARD, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.my_bank_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        if (baseEventBean.code == Constants.TYPE_3) {
            requestData();
        }
        return false;
    }

    @OnClick(R.id.btn_bind)
    public void onViewClicked() {//添加银行卡
        Intent intent = new Intent(this, AddBankCardOneActivity.class);
        startActivity(intent);
    }

    private void initHead() {
        headerview.setTitle("我的银行卡");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    @Override
    public void onDelete() {
        MyDialog myDialog = new MyDialog(this);
        myDialog.setTxtTitle("删除确认");
        myDialog.setTxtContent("您确定要删除该银行卡吗？");
        myDialog.setOnChekedClick(new MyDialog.OnChekedClick() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onConfirmClick() {
                delete();
            }
        });
        myDialog.show();
    }

    /**
     * 删除银行卡
     */
    private void delete() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("card_id", cardID);
        requestHttpLoader.setParams(NetConfig.DELETE_CARD, map);
        requestHttpLoader.showDialog(this, true, "正在删除");
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                ToastUtil.showToast("删除成功");
                requestData();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }
}
